package month1;

import java.util.*;

public class RightSideView199_05 {
    public static void main(String[] args) {
        System.out.println(rightSideView(new TreeNode(new Integer[]{1, 2, 3, null, 5, null, 4})));
    }

    /**
     * 层序遍历
     * @param root
     * @return
     */
    static List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new LinkedList<>();
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (root != null && !q.isEmpty()) {
            for (int i = q.size(); i > 0; --i) {
                TreeNode node = q.poll();
                if (i == 1) {
                    ret.add(node.val);
                }
                if (node.left != null) {
                    q.offer(node.left);
                }
                if (node.right != null) {
                    q.offer(node.right);
                }
            }
        }
        return ret;
    }
}
